﻿using System.Collections.Generic;
using BoatNGoat.Logic;
using BoatNGoat.Logic.Map;
using BoatNGoat.Server;

namespace BoatNGoat.Host
{
    public class BoatNGoatHost : Server.Host
    {
        private readonly Simulation _simulation = new Simulation();

        private readonly Dictionary<PlayerID, Player> _players = new Dictionary<PlayerID, Player>();  

        public BoatNGoatHost()
            : base(20)
        {

        }

        public override void Update(ServerTime serverTime)
        {            
            _simulation.Update(serverTime);
        }

        public override void Init()
        {
            _simulation.Init();
        }

        public IEnumerable<IMobile> GetGoats()
        {
            var goats = _simulation.GetGoats();
            Mobile[] mobiles = new Mobile[goats.Count];
            for (int i = 0; i < mobiles.Length; i++)
            {
                mobiles[i] = new Mobile(goats[i]);
            }
            return mobiles;
        }

        public PlayerID RegisterPlayer()
        {
            PlayerMob player = _simulation.InitPlayer();
            PlayerID id = new PlayerID { ID = _players.Count};
            _players.Add(id, new Player(player));

            return id;
        }

        public IMap GetMap()
        {
            return _simulation.GetMap();
        }
    }
}